package com.MTR;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.text.SimpleDateFormat;

import javax.swing.*;


public class TrainDispLabel extends JComponent {

	
	// for display
	public MoteData[] disp = new MoteData[2];
	
	private int 	index = 0;
	private String	label = null;
	
	public void setDisplay(MoteData attrib, int index)
	{
		if (index <0 || index >=2)
		{
			System.out.println("Out of Bound");
			return ;
		}
		
		disp[index] = attrib;
		// update the UI
		this.repaint();
	}
	
    	
	public TrainDispLabel(int index, String label) {
		super();
		
		this.index = index;
		this.label = label;
		
		this.setLayout(null);
		this.setPreferredSize(new Dimension(120, 90));
		this.setVisible(true);		
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1325689677933735681L;

	@Override
	protected void paintComponent(Graphics g) {

		// draw truck body
		// draw fill color accroding to temperature
		if (disp[0] != null && disp[0].temperature > Setting.errorTemp)
			g.setColor(Color.RED);
		else if (disp[0] != null && disp[0].temperature > Setting.warningTemp)
			g.setColor(Color.YELLOW);
		else
			g.setColor(Color.GREEN);
		
		g.fillRect(10, 10, 90, 60);
		
		// draw border
		g.setColor(Color.BLACK);
		g.drawRect(10, 10, 90, 60);
		
		// drwa wheel
		g.fillOval(20, 60, 20, 20);
		g.fillOval(70, 60, 20, 20);
		g.setColor(Color.WHITE);
		g.fillOval(25, 65, 10, 10);
		g.fillOval(75, 65, 10, 10);
		g.setColor(Color.BLACK);
		
		// draw text
		g.drawString(String.valueOf(this.index), 70, 25);
		g.drawString(this.label, 20, 25);
		if (this.disp[0] != null)
		{
			//g.drawString(String.valueOf(disp[0].vehicleID), 20, 25);
			
			// format date time
			String dispTm = new SimpleDateFormat("MM-dd HH:mm").format(disp[0].sampleDate);
			g.drawString(dispTm, 20, 40);
			g.drawString(disp[0].temperature + "C", 20, 55);
		} // end if			
	
	} // end paint

	@Override
	protected void paintBorder(Graphics g) {
		Dimension dv = this.getSize();
		g.setColor(Color.BLACK);
		g.drawRect(0, 0, dv.width-1, dv.height-1);
	}


}
